//+------------------------------------------------------------------+
//|                                                         1122.mq5 |
//|                                  Copyright 2025, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

// 交易参数
input int      MagicNumber = 12345;   // EA魔术号


// 画线参数
input int      StartBarForLines = 30;     // 画线起始K线位置
input int      EndBarForLines = 60;       // 画线结束K线位置
input color    HighLineColor = clrRed;    // 最高价线颜色
input color    LowLineColor = clrGreen;   // 最低价线颜色
input int      LineWidth = 2;             // 线宽


double         lastHighest = EMPTY_VALUE; // 记录上一次的最高价
double         lastLowest = EMPTY_VALUE;  // 记录上一次的最低价

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
//---

   DrawRangeLines(); // 初始化时画线
//---
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| 绘制价格区间线函数                                                |
//+------------------------------------------------------------------+
void DrawRangeLines()
{
   // 检查参数有效性
   if(StartBarForLines <= 0 || EndBarForLines <= 0 || EndBarForLines <= StartBarForLines)
   {
      Print("画线参数错误：起始/结束位置无效");
      return;
   }
   
   // 计算需要复制的K线数量
   int count = EndBarForLines - StartBarForLines + 1;
   if(count <= 0) return;
   
   // 获取历史数据
   double highs[], lows[];
   int copiedHigh = CopyHigh(_Symbol, _Period, StartBarForLines, count, highs);
   int copiedLow = CopyLow(_Symbol, _Period, StartBarForLines, count, lows);
   
   if(copiedHigh <= 0 || copiedLow <= 0)
   {
      Print("获取历史数据失败！错误代码=", GetLastError());
      return;
   }
   
   // 计算最高价和最低价
   double highest = highs[ArrayMaximum(highs, 0, copiedHigh)];
   double lowest = lows[ArrayMinimum(lows, 0, copiedLow)];
   
   // 如果价格未变化且线条已存在，则无需更新
   if(lastHighest != EMPTY_VALUE && lastLowest != EMPTY_VALUE &&
      MathAbs(highest - lastHighest) < _Point && 
      MathAbs(lowest - lastLowest) < _Point)
   {
      return;
   }
   
   // 更新记录的价格
   lastHighest = highest;
   lastLowest = lowest;
   
   // 删除旧线条
   ObjectDelete(0, "HighLine_"+IntegerToString(MagicNumber));
   ObjectDelete(0, "LowLine_"+IntegerToString(MagicNumber));
   
   // 创建新高价线
   if(!ObjectCreate(0, "HighLine_"+IntegerToString(MagicNumber), OBJ_HLINE, 0, 0, highest))
   {
      Print("创建最高价线失败！错误代码=", GetLastError());
   }
   else
   {
      ObjectSetInteger(0, "HighLine_"+IntegerToString(MagicNumber), OBJPROP_COLOR, HighLineColor);
      ObjectSetInteger(0, "HighLine_"+IntegerToString(MagicNumber), OBJPROP_WIDTH, LineWidth);
      ObjectSetInteger(0, "HighLine_"+IntegerToString(MagicNumber), OBJPROP_STYLE, STYLE_SOLID);
   }
   
   // 创建新低价线
   if(!ObjectCreate(0, "LowLine_"+IntegerToString(MagicNumber), OBJ_HLINE, 0, 0, lowest))
   {
      Print("创建最低价线失败！错误代码=", GetLastError());
   }
   else
   {
      ObjectSetInteger(0, "LowLine_"+IntegerToString(MagicNumber), OBJPROP_COLOR, LowLineColor);
      ObjectSetInteger(0, "LowLine_"+IntegerToString(MagicNumber), OBJPROP_WIDTH, LineWidth);
      ObjectSetInteger(0, "LowLine_"+IntegerToString(MagicNumber), OBJPROP_STYLE, STYLE_SOLID);
   }
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
      DrawRangeLines();
}

//+------------------------------------------------------------------+
//| 程序去初始化函数                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   // 删除绘制的线条
   ObjectDelete(0, "HighLine_"+IntegerToString(MagicNumber));
   ObjectDelete(0, "LowLine_"+IntegerToString(MagicNumber));
}
//+------------------------------------------------------------------+